//
//  YDOAInsuranceVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/6/8.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit

class YDOAInsuranceVC: UIViewController {

    @IBOutlet weak var myTableView: UITableView!
    
    lazy var statusArr : Array  = Array<Int>() //保存所有头部的状态
    lazy var headerViews : Array = Array<UIView>() //保存所有头部视图
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myTableView.register(UINib(nibName: "YAODCatOilcell", bundle: nil), forCellReuseIdentifier: "YAODCatOilcell")
   
        for _ in 0..<20 {
            statusArr.append(0)
        }

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //发起申请按钮点击
    @IBAction func sureBtnClick(_ sender: Any) {
        
        navigationController?.pushViewController(YDOAInsuranceApplyVC(), animated: true)
   
    }

   
}

extension  YDOAInsuranceVC : UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 20
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if statusArr[section] == 0{
            return 0
        }
        return  1
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 5
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "YAODCatOilcell") as?  YAODCatOilcell  else {
            return YAODCatOilcell()
        }
        
        cell.index = indexPath.section
        
        cell.callback = {(id : Int , index : Int) in
            
            YMLog.log(item: "点击了id = \(id) 下标 \(index) 行")
            
        }
        //cell.textLabel?.text = "这是第\(indexPath.section)行"
        return  cell
        
        
    }
    
    
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        //油卡充值部分的头部
  
            //删除所有视图
            //        let reuserId = "herderView"
            //        let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: reuserId)
            //        if headerView == nil {
            let view1 = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 40))
            
            let oilnumLab = UILabel(frame: CGRect(x: 15, y: 20, width: ScreenWidth - 30, height: 21))
            oilnumLab.text = "保险车辆：劳斯莱斯幻影"
            oilnumLab.textColor = UIColor.black
            oilnumLab.font = UIFont.systemFont(ofSize: 15)
            
            let payCountLab = UILabel(frame: CGRect(x: 15, y: oilnumLab.ym_bottomY+8, width:ScreenWidth - 30, height: 21))
            payCountLab.text = "车牌号：渝A 000001"
            payCountLab.textColor = UIColor.black
            payCountLab.font = UIFont.systemFont(ofSize: 15)
            
            let timeLab = UILabel(frame: CGRect(x: 15, y: payCountLab.ym_bottomY + 8, width: ScreenWidth - 30, height: 21))
            timeLab.text = "保险金额：50,000,000"
            timeLab.textColor = UIColor.black
            timeLab.font = UIFont.systemFont(ofSize: 15)
            
            
            view1.tag = 500 + section
            let tap = UITapGestureRecognizer(target: self, action: #selector(click))
            view1.addGestureRecognizer(tap)
            let  lineView = UIView(frame: CGRect(x: 0, y: 117, width: ScreenWidth, height: 1))
            lineView.backgroundColor = UIColor.ym_colorWithRGB(r: 222, g: 222, b: 222)
            view1.addSubview(lineView)
            view1.backgroundColor = UIColor.white
            
            view1.addSubview(oilnumLab)
            view1.addSubview(payCountLab)
            view1.addSubview(timeLab)
            
            if headerViews.count < statusArr.count{
                headerViews.append(view1)
            }
            
            return view1
    }
    
    //MARK: - 点击事件
    @objc private func click(tap : UIGestureRecognizer){
        
      
            let tag = (tap.view?.tag)! - 500
            if statusArr[tag] == 0 {
                statusArr[tag] = 1
            }else{
                statusArr[tag] = 0
            }
            headerViews.removeAll()
            myTableView.reloadData()
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 118
        
    }


}







